﻿using CaptureImage;
using OcrWinForm.VO;
using PaddleOCRSharp;
using System.Text.Json;


namespace OcrWinForm.Controls
{
    public partial class OcrControl : UserControl
    {
        public OcrControl()
        {
            InitializeComponent();
        }
        private PaddleOCREngine engine;
        private void btn1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
            if (ofd.ShowDialog() != DialogResult.OK) return;


            //  this.label1.Text = ofd.FileName;
            this.pictureBox1.Image = Image.FromFile(ofd.FileName);
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("请先选择或截取图像", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            };
            Bitmap imagebyte = new Bitmap(pictureBox1.Image);

            // OCR参数
            OCRModelConfig config = null;
            OCRParameter oCRParameter = new()
            {
                cpu_math_library_num_threads = 10, // 预测并发线程数
                enable_mkldnn = true, // web部署该值建议设置为0,否则出错，内存如果使用很大，建议该值也设置为0.
                cls = false, // 是否执行文字方向分类；默认false
                det = true, // 是否开启方向检测，用于检测识别180旋转
                use_angle_cls = false, // 是否开启方向检测，用于检测识别180旋转
                det_db_score_mode = true, // 是否使用多段线，即文字区域是用多段线还是用矩形
                max_side_len = 1500,
                rec_img_h = 48,
                rec_img_w = 320,
                det_db_thresh = 0.3f,
                det_db_box_thresh = 0.618f
            };

            // 初始化OCR引擎
            engine = new PaddleOCREngine(config, oCRParameter);

            // 模型配置，使用默认值
            StructureModelConfig structureModelConfig = null;

            // 表格识别参数配置，使用默认值
            StructureParameter structureParameter = new StructureParameter();
            PaddleStructureEngine structengine = new PaddleStructureEngine(structureModelConfig, structureParameter);

            // 执行OCR识别
            OCRResult ocrResult = engine.DetectText(imagebyte);

            //判断是否换行
            if (this.switch1.Checked)
            {
                richTextBox1.Text = "";
                List<TxetItem> items = JsonSerializer.Deserialize<List<TxetItem>>(ocrResult.JsonText);
                items.ForEach(x => richTextBox1.AppendText(x.Text + Environment.NewLine));
            }
            else
            {
                richTextBox1.Text = ocrResult.Text;
            }
        }
        private void btn2_Click(object sender, EventArgs e)
        {
            CaptureImageTool capture = new();
            if (capture.ShowDialog() == DialogResult.OK)
            {
                Image image = capture.Image;
                pictureBox1.Image = image;
            }
        }

        private void switch1_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            if (switch1.Checked)
            {
                this.label2.Text = "换行开启";
            }
            else
            {
                this.label2.Text = "换行关闭";
            }
        }
    }
}
